package com.xindacloud.sms.portal.utils;

import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


@Data
@Builder
public class UnionResultHelp<T> implements Serializable {

    public enum StatusEnum {
        /**
         * 成功
         */
        Success(200),
        /**
         * 系统降级
         */
        HystrixDegradation(300),
        /**
         * 熔断
         */
        HystrixFusing(301),
        /**
         * 内部错误
         */
        InnerError(500),
        /**
         * 无权访问
         */
        Forbidden(403),
        /**
         * 业务异常
         */
        Fail(420),
        /**
         * 未发现Token
         */
        FailToken(401),
        /**
         * 用户名或密码错误
         */
        FailLogin(402),
        /**
         * 验证码错误
         */
        FailVCode(403),
        /**
         * 资源不存在
         */
        NotFound(404),
        /**
         * 表单错误
         */
        FormFail(405),
        /**
         * 未知错误
         */
        Unknow(0),
        /**
         * 参数错误
         */
        ParamsFail(406);

        private int code;

        StatusEnum(int code) {
            this.code = code;
        }

        public int code() {
            return code;
        }

        @Override
        public String toString() {
            return super.toString();
        }
    }


    /**
     * 状态
     */
    private Integer status;

    /**
     * 返回的数据
     */
    private T data;


    /**
     * 消息,通常是错误状态下的错误消息
     */
    private String message;

    /**
     * 时间
     */
    private String timestamp;

    public Boolean isValid = false;

    public static String getStrTimestamp() {
        return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LocalDateTime.now());
    }

    public UnionResultHelp() {
    }

    public UnionResultHelp(T data) {
        this.status = StatusEnum.Success.code();
        this.data = data;
        this.timestamp = getStrTimestamp();
        this.message = StatusEnum.Success.toString();
    }

    public UnionResultHelp(StatusEnum status, T data) {
        this.status = status.code();
        this.data = data;
        this.timestamp = getStrTimestamp();
        if (this.status == 200) {
            this.message = StatusEnum.Success.toString();
        } else {
            this.message = StatusEnum.Fail.toString();
        }

    }

    public UnionResultHelp(int status, T data) {
        this.status = status;
        this.data = data;
        this.timestamp = getStrTimestamp();
        if (this.status == 200) {
            this.message = StatusEnum.Success.toString();
        } else {
            this.message = StatusEnum.Fail.toString();
        }

    }

    public UnionResultHelp(StatusEnum status, String message) {
        this.status = status.code();
        this.message = message;
        this.timestamp = getStrTimestamp();
        if (this.status == 200) {
            this.message = StatusEnum.Success.toString();
        } else {
            this.message = StringUtils.isNotEmpty(message) ? message : StatusEnum.Fail.toString();
        }

    }

    public UnionResultHelp(Integer status, T data, String message, String timestamp, Boolean isValid) {
        this.status = status;
        this.data = data;
        this.message = message;
        this.timestamp = timestamp;
        this.isValid = isValid;
    }
}
